package com.zy.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.zy.common.util.HttpClientUtil;
import com.zy.common.util.TaotaoResult;
import com.zy.pojo.TbItem;
import com.zy.pojo.TbItemDesc;
import com.zy.portal.pojo.ItemInfo;
import com.zy.portal.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {

	/*
	 * 基础url
	 */
	@Value("${REST_BASE_URL}")
	private String REST_BASE_URL;
	/*
	 * 商品url
	 */
	@Value("${ITEM_INFO_URL}")
	private String ITEM_INFO_URL;
	/*
	 * 商品描述url
	 */
	@Value("${ITEM_DESC_URL}")
	private String ITEM_DESC_URL;

	@Override
	public TbItem getItemById(Long itemId) {
		try {
			// get post此处不做要求
			// 从缓存中提取数据，使用RPC远程调用方式
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_INFO_URL + itemId);
			if (!StringUtils.isBlank(json)) {
				// 将字符串转成封装的对象，方便获取到值
				TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json,TbItem.class);
				if (taotaoResult.getStatus() == 200) {
					// 取出响应中的数据
					TbItem tbItem = (TbItem) taotaoResult.getData();
					return tbItem;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	// 商品描述
	@Override
	public String getItemDescById(Long itemId) {
		try {
			// 商品描述
			String json = HttpClientUtil.doGet(REST_BASE_URL + ITEM_DESC_URL + itemId);
			// 转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, TbItemDesc.class);
			if (taotaoResult.getStatus() == 200) {
				TbItemDesc itemDesc = (TbItemDesc) taotaoResult.getData();
				String result = itemDesc.getItemDesc();
				return result;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
